package group2projman;

import java.sql.Connection;
import java.sql.ResultSet;

class User implements DbWritable {
	
	public enum Permission {
		MEMBER, STAFF, ADMINISTRATOR
	}
	
	String name, password;
	
	Permission permission;
	
	private	int id;
	
	public User(int pId, Permission pPermission) {
		id = pId;
		permission = pPermission;
	}
		
	public int getId() {
		return id;
	}
	
	public String listAttributes() {
		StringBuffer result = new StringBuffer();
		
		result.append("int:").append("id;").append(id);
		result.append(",str:").append("name;").append(name);
		result.append(",str:").append("password;").append(password);
		result.append(",str:").append("permission;").append(permission.toString());
		
		return result.toString();
	}
	
	public boolean loadAttributes(String desc) {
		try {
			String[] split = desc.split("[;,]");	
			
			if (!split[0].equals("int:id"))
				return true;	
			id = Integer.parseInt(split[1]);
			
			if (!split[2].equals("str:name"))
				return true;	
			name = split[3];
			
			if (!split[4].equals("str:password"))
				return true;	
			password = split[5];
			
			if (!split[6].equals("str:permission"))
				return true;	
			permission = Permission.valueOf(split[7]);					
		}  catch (Exception e) {
			// Array index out of bounds or enum value not found
			return true;
		}
		
		return false;
	}
	
	
	
	/*public void save(Connection connection) {
		
		
		StringBuffer statement = new StringBuffer();
		
		statement.append("INSERT INTO users VALUES('");
	
		
		/*connection.createStatement().executeUpdate("INSERT INTO users VALUES('" );
				
				//', 2)");
		
		connection.createStatement().executeUpdate("UPDATE fruit SET flavor='9' WHERE name='Pineapple'");
		
		ResultSet result = connection.createStatement().executeQuery("SELECT * from fruit");
		
		while (result.next())
			System.out.println(result.getString("name") + " " + result.getInt("flavor"));*/
	//}

}
